//
//  RecommendViewModel.swift
//  SwiftDemo
//
//  Created by liguoqing on 2022/6/29.
//  Copyright © 2022 guoqing.li. All rights reserved.
//

import UIKit

class RecommendViewModel : BaseViewModel {
    lazy var cycleModels : [CycleModel] = [CycleModel]()
    private lazy var bigDataGroup:AnchorGroupModel = AnchorGroupModel()
    private lazy var prettyDataGroup:AnchorGroupModel = AnchorGroupModel()
}

extension RecommendViewModel
{
    func requestData(finishCallBack : @escaping () -> ()){
        // 定义参数
        let params = ["limint":"4","offset":"0","time":NSDate.getCurrentTime() as String];
        // 创建 grooup
        let dispatchGroup = DispatchGroup()
        // 1.请求热门数据
        // 进入组
        dispatchGroup.enter()
        NetWorkTools.requestData(type: .get, URLString: "http://capi.douyucdn.cn/api/v1/getbigDataRoom", parameters: ["time" : NSDate.getCurrentTime() as String]) { result in
            // result -> dict
            guard let resutDict = result as? [String : NSObject] else { return }
            // 获取数据
            guard let dataArray = resutDict["data"] as? [[String : NSObject]] else { return }
            // 字典转模型
            self.bigDataGroup.tag_name = "热门"
            self.bigDataGroup.icon_name = "home_header_hot"
            
            for dict in dataArray {
                self.bigDataGroup.anchors.append(AnchorModel.init(dic: dict))
            }
            dispatchGroup.leave()
        }
        
        // 2.颜值
        dispatchGroup.enter()
        NetWorkTools.requestData(type: .get, URLString: "http://capi.douyucdn.cn/api/v1/getVerticalRoom", parameters: params) { result in
            
            guard let resutDict = result as? [String : NSObject] else { return }
            
            guard let dataArray = resutDict["data"] as? [[String : NSObject]] else { return }
            
            self.prettyDataGroup.tag_name = "颜值"
            self.prettyDataGroup.icon_name = "home_header_phone"
            
            for dict in dataArray {
                self.prettyDataGroup.anchors.append(AnchorModel.init(dic: dict))
            }
            dispatchGroup.leave()
        }
        
        // 3.其他
        dispatchGroup.enter()
        loadAnchorModel(isGroupData: true, URLString: "http://capi.douyucdn.cn/api/v1/getHotCate", parameters: params) {
            dispatchGroup.leave()
        }
        
        // 4.所有数据请求完毕，进行排序
        dispatchGroup.notify(queue: .main){
            self.anchorGroups.insert(self.prettyDataGroup, at: 0)
            self.anchorGroups.insert(self.bigDataGroup, at: 0)
            
            finishCallBack()
        }
    }
    func requestCycleData(finishCallBack: @escaping () -> ()){
        NetWorkTools.requestData(type: .get, URLString: "http://capi.douyucdn.cn/api/v1/slide/6", parameters: ["version":"2.300"]) { result in
            
            guard let resutDict = result as? [String : NSObject] else { return }
            
            guard let dataArray = resutDict["data"] as? [[String : NSObject]] else { return }
            
            for dict in dataArray {
                self.cycleModels.append(CycleModel(dict: dict))
            }
            
            finishCallBack()
        }
    }
}




